// pages/uploadShopp/uploadShopp.js
var app = getApp();
var util = require('../../utils/util');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        curHdIndex: 0,
        goods_image:'',
		if_bao:2
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
    },
    //tab切换
    tab: function (e) {
        this.setData({
            curHdIndex:  e.currentTarget.id,
        })
    },
	bindPickerChange:function(e){
		this.setData({
			index:e.detail.value,
		});
	},
    getClassList:function(){
        var that = this;
        util.showLoading();
        util.fetchPost("/api/store/store/goods_class_list", {
        }, function (res) {
            if (res.state == 1) {
                var array = [];
                for (var i in res.datas) {
                    array.push(res.datas[i].name)
                }
                that.setData({
                    array:array,
                    index:0,
                    classlist: res.datas,
                })
            } else {
                util.message(res.datas)
            }
        })
        util.hideLoading();
    },
    del_class:function(e){
        var class_id = e.currentTarget.dataset.id;
        var that = this;
        util.confirm('您确定删除分类吗？删除后不可恢复',function(res){
            if(res.confirm){
                util.showLoading();
                util.fetchPost("/api/store/store/del_class", {
                    class_id:class_id
                }, function (res) {
                    if (res.state == 1) {
                        util.showSuccess(res.datas);
                        setTimeout(function(){
                            that.getClassList();
                        },1000);
                    } else {
                        util.message(res.datas)
                    }
                })
                util.hideLoading();
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },
    uploadImage:function(){
        var that = this;
        wx.chooseImage({
            success(res) {
                util.showLoading("图片上传中");
                const tempFilePaths = res.tempFilePaths
                wx.uploadFile({
                    url: app.globalData.http_url + '/api/v1/common/upload', //仅为示例，非真实的接口地址
                    header: {
                        'Content-Type': 'multipart/form-data'
                    },
                    filePath: tempFilePaths[0],
                    name: 'file',
                    formData: {
                        key: app.globalData.key
                    },
                    success: function (result) {
                        util.hideLoading();
                        var image_all = that.data.image_all || [];
                        //上传图片后返回的是字符串，需json.prise一下
                        var res = JSON.parse(result.data);
                        that.setData({
                            goods_image:res.path,
                            image:res.id,
                        });
                    },
                    fail: function (res) {
                        utils.hideLoading();
                        utils.message('上传服务器上传失败,请稍后再试2');
                        console.log(res);
                    }
                })
            }
        })
    },
	onCloseImgTap: function (e) {
		var that = this;
		that.setData({
			goods_image: "",
		});
	},

	switch1Change:function(e){
		var  that=this;
		if (e.detail.value==true){
			that.setData({
				if_bao:1
			})
		}else{
			that.setData({
				if_bao: 2
			})
		}
		},
    formSubmit:function(e){
        var that = this;
        var name = e.detail.value.name;
        var price = e.detail.value.price;
        var sort = e.detail.value.sort;
		 var stock = e.detail.value.stock;
        if(name.length < 1){
            util.showSuccess('请输入商品名称','none',false,800);
            return false;
        }
        var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
        var money = "520.100";
        if (!reg.test(price)) {
            util.showSuccess('请填写正确的商品价格','none',false,800);
            return false;
        };
        if(sort.length < 1){
            util.showSuccess('请输入排序','none',false,800);
            return false;
        }
		if (stock.length <= 0) {
			util.showSuccess('请输入库存', 'none', false, 800);
			return false;
		}
		if (stock == 0) {
			util.showSuccess('库存不能小于0', 'none', false, 800);
			return false;
		}
        if(that.data.goods_image == ''){
            util.showSuccess('请上传商品图片','none',false,800);
            return false;
        }
		
        util.showLoading();
		var apiUrl = '/api/store/store/add_modify_goods_v3';
        util.fetchPost(apiUrl, {
            gc_id: that.data.classlist[that.data.index].id,
            name: name,
            price: price,
            image: that.data.image,
            sort: sort,
			stock: stock,
			if_bao:that.data.if_bao
        }, function (res) {
            util.hideLoading();
            if(res.state==1){
                util.showSuccess(res.datas);
                setTimeout(function(){
                    wx.navigateBack();
                },2000);
            }else{
                util.message(res.datas);
            }
        })
    },
    //修改分类  //添加分类
    modifyTap:function(e){
        if(e.currentTarget.dataset.id != undefined){
            wx.navigateTo({
                url: '../modify/modify?class_id=' + e.currentTarget.dataset.id
            })
        }else{
            wx.navigateTo({
                url: '../modify/modify'
            })
        }
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var that = this;
        that.getClassList();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})